<?php

require_once './config.inc';
require_once './_common.inc';

function secret_split_array($string, $n){
	
	if(strlen($string) > 48){
		echo "<b><i>ERROR: Input string to long.</b></i>";
		die;
	}
	
	$s_array = str_split($string);
	
	for($i = 0; $i < $n; $i++){
		
		for($letter = 0; $letter < sizeof($s_array); $letter++){
			
			$left[$letter] = mt_rand(0, 127);
			$right[$letter] = ord($s_array[$letter]) ^ $left[$letter];
		
		
			if($left[$letter] < 10)
				$left[$letter] = (string)"00" . $left[$letter];
			elseif ($left[$letter] < 100)
				$left[$letter] = (string)"0" . $left[$letter];
			
			if($right[$letter] < 10)
				$right[$letter] = (string)"00" . $right[$letter];
			elseif ($right[$letter] < 100)
					$right[$letter] = (string)"0" . $right[$letter];
		}		
			
		$id[$i] = array(0 => implode($left), 1 => implode($right) );
	}	
	
	return $id;
}

function commit_id_strings($id){
	
	for($i = 0; $i < sizeof($id); $i++){
		
		$commit[$i][0] = md5($id[$i][0]);
		$commit[$i][1] = md5($id[$i][1]);
	}
	
	return $commit;
	
}

function generate_r($n){
	
	$r = '0';
	while((int)gmp_strval(gmp_gcd($r,$n)) != 1 || (int)$r == 0){
		$r = '';
		while(strlen($r) < (strlen($n) - 1)){
			$r .= (string)mt_rand(0,9);
		}
	}
	return $r;	
}

function generate_us(){
	
	$us = '';
	while(strlen($us) < 20){
		
		$char = (string)mt_rand(0,35);
		if((int)$char > 9)
			$char = chr($char + 55);
		
		$us .= $char;
	}
	
	return $us;
}


/*
 * Function to return committed string from table mo_idstrings_customer
 */
function form_id_strings($uniqueStr, $conn){
	//get ID strings
 	$IDstrs = db_do_query($conn,"SELECT * FROM mo_idstrings_customer where uniqueness_string='" .$uniqueStr ."'");
 	for ($i=0; $i < sizeof($IDstrs); $i++){
 		//echo $IDstrs[$i][L_ID_STRING];
 		$ID[$IDstrs[$i][IDSTRING_N]-1][0] = md5($IDstrs[$i][L_ID_STRING]);
 		$ID[$IDstrs[$i][IDSTRING_N]-1][1] = md5($IDstrs[$i][R_ID_STRING]);
 	}
 	//print_r($mo['id_hash']);
  	
 	return $ID;
}

?>